﻿/*
 *  Author David 2012-9-13 10:33
 *  业务逻辑检查正常
 * 
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class TimeSharingController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /TimeSharing/

        public ActionResult Index(int uid, string pdate)
        {

            BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
            th = th.GetSettlementModel(pdate, db);//查询数据库
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            cookie.SetCookie("querydate", pdate, 20);
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.VisitPrower.HasOperatorPrower(operId, uid, db))
            {
                if (th != null && th.YPdate != 0)
                {
                    int tid = th.Id;
                    int operatorId = uid;
                    BenefitHelper.Sys.Operator op = new BenefitHelper.Sys.Operator().GetModel(operatorId, db);
                    ViewBag.op = op;
                    ViewBag.pdate = pdate;
                    string stringPdate = new BenefitHelper.Sys.TradeHistory().AddHorizontalLineIntoPdate(pdate);
                    ViewBag.Hpdate = stringPdate;
                    bool traded = op.OperatorTradedAtDay(operatorId, tid, db);

                    BenefitHelper.Sys.TradeHistory preDay = th.GetPreviousTradingDay(tid, db);
                    BenefitHelper.Sys.TradeHistory preWeekLastDay = th.GetPreviousWeekLastTradingDay(tid, db);

                    if (traded)
                    {

                        BenefitHelper.View.OperatorDayChange change = new BenefitHelper.View.OperatorDayChange();
                        change = change.GetOperatorDayChange(uid, tid, db);//交易员日数据
                        ViewBag.change = change;

                        BenefitHelper.View.OperatorDayChange prechange = new BenefitHelper.View.OperatorDayChange();
                        prechange = prechange.GetOperatorDayChange(uid, preDay.Id, db);//交易员上日数据
                        ViewBag.prechange = prechange;



                        BenefitHelper.View.OperatorAvgWeek changeweek = new BenefitHelper.View.OperatorAvgWeek().GetOperatorAvgWeek(operatorId, th.YPdate, th.WPdate, db);//交易员周均值
                        ViewBag.changeweek = changeweek;

                        BenefitHelper.View.OperatorAvgWeek prechangeweek = new BenefitHelper.View.OperatorAvgWeek().GetOperatorAvgWeek(operatorId, preWeekLastDay.YPdate, preWeekLastDay.WPdate, db);//交易员上周周均值
                        ViewBag.prechangeweek = prechangeweek;


                        BenefitHelper.View.OperatorFiveDayAvg changefiveday = new BenefitHelper.View.OperatorFiveDayAvg().GetOperatorFiveDayAvg(operatorId, tid, db);//前五日均值
                        ViewBag.changefiveday = changefiveday;

                        BenefitHelper.View.OperatorFiveDayAvg prechangefiveday = new BenefitHelper.View.OperatorFiveDayAvg().GetOperatorFiveDayAvg(operatorId, preDay.Id, db);//上日的前五日均值
                        ViewBag.prechangefiveday = prechangefiveday;

                        //BenefitHelper.View.OperatorWeekSum operatorweeksum = new BenefitHelper.View.OperatorWeekSum().GetOperatorWeekSum(operatorId, th.YPdate, th.MPdate, th.WPdate, db);//周累计
                        //ViewBag.operatorweeksum = operatorweeksum;

                        string js = BenefitHelper.Tools.ChartAtManager.GetChartBillCountOnCCFX(tid, operatorId, db);
                        js += BenefitHelper.Tools.ChartAtManager.GetChartYieldOnCCFX(tid, operatorId, db);
                        ViewBag.js = js;
                        db.Dispose();
                        return View();
                    }
                    else
                    {
                        db.Dispose();
                        return Redirect("/Error/index/" + uid + "/" + pdate);
                    }
                }
                else
                {
                    db.Dispose();
                    return Redirect("/Error/index/" + uid + "/" + pdate);
                }
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
        }

    }
}
